# if 条件判断；match：模式匹配

age = 3
if age >= 18:
    print('adult')
elif age >= 6:
    print('teenager')
else:
    print('kid')

# bmi计算
# height = input('请输入身高cm: ')
# weight = input('请输入体重kg: ')
height = 170
weight = 72
# input输入是字符串，float() 转换为浮点数，**表示幂次方，int() 转换为整数
bmi = float(weight) / (float(height) / 100)**2
print('bmi:', bmi)
if bmi < 18.5:
    print('过轻')
elif bmi < 25:
    print('正常')
elif bmi < 28:
    print('过重')
elif bmi < 32:
    print('肥胖')
else:
    print(' severely obese')


# match模式匹配 支持模式解构、类型匹配、守卫条件等高级特性
print('------------------')
print('match模式匹配:')
score = 'B'
match score:
    case 'A':
        print('优秀')
    case 'B':
        print('良好')
    case 'C':
        print('及格')
    case 'D':
        print('不及格')
    case _: # 匹配任意值
        print('Score is ?????.')

#  复杂匹配
age = 3
match age:
    case x if x < 10: # 匹配条件，if条件, x为匹配的值
        print(f'< 10 years old, age is {x}')
    case 10:
        print('10 years old')
    case 11|12|13|14|15|16|17|18: # 匹配多个值
        print('10-18 years old')
    case _: # 匹配任意值
        print('> 18 years old')

# 解构匹配（匹配结构）
point = (0, 1)
match point:
    case (0, 0):
        print("Origin")
    case (x, 0):
        print(f"On x-axis at {x}")
    case (0, y):
        print(f"On y-axis at {y}")
    case (x, y):
        print(f"Point at ({x}, {y})")

# 匹配列表
args = ['gcc', 'hello.c', 'world.c']
# args = ['clean']
# args = ['gcc']
match args:
    # 如果仅出现gcc，报错:
    case ['gcc']:
        print('gcc: missing source file(s).')
    # 出现gcc，且至少指定了一个文件:
    case ['gcc', file1, *files]:   # *files为匹配到的多个文件
        print('gcc compile: ' + file1 + ', ' + ', '.join(files))
    # 仅出现clean:
    case ['clean']:
        print('clean')
    case _:
        print('invalid command.')

print('------------------')
# 匹配字典
data = {"name": "Alice", "age": 30}
match data:
    case {"name": str(name), "age": int(age)} if age >= 18:
        print(f"Adult: {name}")
    case {"name": str(name)}:
        print(f"Name only: {name}")

 # 类型匹配
def describe(value):
    match value:
        case int(n) if n > 0:
            return f"Positive integer: {n}"
        case str(s) if s.isupper():
            return f"Uppercase string: {s}"
        case _:
            return "Something else"

print(describe(42))       # Positive integer: 42
print(describe("HELLO"))  # Uppercase string: HELLO